//
//  ProAndCityViewController.m
//  09-Homework
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ProAndCityViewController.h"
#import "ProModel.h"
#import "AreaModel.h"
#import "CityModel.h"
#import "AreaViewController.h"
#define kTableViewReuse @"reuseIndentifier"
@interface ProAndCityViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *proArr;
@end

@implementation ProAndCityViewController
#pragma mark - 第三题
- (void)viewDidLoad {
    [super viewDidLoad];
    self.proArr = [NSMutableArray array];
    [self getData];
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}

- (void)getData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"];
    NSString *sourceStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [sourceStr componentsSeparatedByString:@"\n"];
    for (NSString *str in arr) {
        if (![str hasPrefix:@" "]) {
            ProModel *pro = [ProModel areaModelWithName:str];
            [_proArr addObject:pro];
        } else if ([str hasPrefix:@"  "] && ![str hasPrefix:@"    "]) {
            ProModel *pro = [_proArr lastObject];
            CityModel *city = [CityModel areaModelWithName:str];
            [pro.arr addObject:city];
        } else {
            ProModel *pro = [_proArr lastObject];
            CityModel *city = [pro.arr lastObject];
            AreaModel *area = [AreaModel areaModelWithName:str];
            [city.arr addObject:area];
        }
    }
}
#pragma mark - TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _proArr.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [_proArr[section] name];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTableViewReuse];
    }
    ProModel *pro = _proArr[indexPath.section];
    cell.textLabel.text = [pro.arr[indexPath.row] name];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_proArr[section] arr].count;
}
#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AreaViewController *areaVC = [[AreaViewController alloc] init];
    ProModel *proModel = _proArr[indexPath.section];
    CityModel *cityModel = proModel.arr[indexPath.row];
    areaVC.cityModel = cityModel;
    [self.navigationController pushViewController:areaVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
